// Keepfit/pages/me/me.js
const app=getApp();
const db = wx.cloud.database({
  throwOnNotFound: false
})
const ui = wx.getStorageSync("userInfo")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},//这个应该y
    openid:"",
    isVIP:true,

    //用户历史参与课程的数量
    participatedNum:0,//参与的课程数量
    pointsNum:0,//关注的数量
    likesNum:0,//
  },

  onGotUserInfo: function(e) {//
    let that = this
    wx.getUserInfo({
      success: res => {
        that.setData({
            userInfo: res.userInfo,
          }),
          console.log(res.userInfo)
          wx.cloud.callFunction({
            //调用云函数获取openid
            name: "login",
            success: res => {
              that.setData({ 
                  openid: res.result.openid
                }),
                //进行初始化操作
                that.data.userInfo.openid = that.data.openid,
                wx.setStorageSync("userInfo", that.data.userInfo),
                
                wx.cloud.callFunction({
                  name: "addUser",
                  data: {
                    //将用户信息保存在本地上
                    nickName: that.data.userInfo.nickName,
                    city: that.data.userInfo.city,
                    country: that.data.userInfo.country,
                    gender: that.data.userInfo.gender,
                    language: that.data.userInfo.language,
                    avatarUrl: that.data.userInfo.avatarUrl,
                    openid: that.data.userInfo.openid,
                    provience: that.data.userInfo.provience
                  },
                })
            },
            fail: res => {
              console.log(res)
              return;
            }
          })
      },
      fail: res => {
        console.log(res)
        return
      }
    })
  },


  // 写改个人信息

  editInfo(){
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      userInfo: ui,
      openid: ui.openid,
      name:ui.nickName,
      navHeight: app.globalData.navHeight,
      navTop: app.globalData.navTop,
      windowHeight: app.globalData.windowHeight,
      windowWidth: app.globalData.windowWidth,
      navLeft: app.globalData.navLeft,
      navMenuHeight: app.globalData.navMenuHeight,
      navMenuWidth: app.globalData.navMenuWidth,
      pixelRatio: app.globalData.pixelRatio,
      tagTop: app.globalData.navTop * app.globalData.pixelRatio
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    db.collection("User").where({
      openid:ui.openid
    }).field({
      name: true,
    }).get().then(res => {
      console.log(res)
      this.setData({
        name:res.data[0].name
      })
    })


    console.log(ui.nickName)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})